import * as Cesium from '@cesiumjs';

/* 81 */
/***/
var createGroundRectangle = (function() {

  // var _toConsumableArray2 = __webpack_require__(8);
  // var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
  var getRectangleWorldPositions = function() {
    var scratchEnuMatrix;
    var scratchRotationQuat;
    var scratchSrtMatrix;
    var ccfe;

    function getRectangleWorldPositions(center, width, height, ox, oy, rotation, result) {

      scratchEnuMatrix = scratchEnuMatrix || new Cesium.Matrix4();
      scratchRotationQuat = scratchRotationQuat || new Cesium.Quaternion();
      scratchSrtMatrix = scratchSrtMatrix || new Cesium.Matrix4();
      ccfe = ccfe || Cesium.Cartesian3.fromElements;
      var localPositions = [ccfe(-ox, -oy, 0.0), ccfe(1.0 - ox, -oy, 0.0), ccfe(1.0 - ox, 1.0 - oy, 0.0), ccfe(-
        ox, 1.0 - oy, 0.0)];

      var enuMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(center, Cesium.Ellipsoid.WGS84,
        scratchEnuMatrix);

      var rotationQuat = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_Z, rotation,
        scratchRotationQuat);
      var cmftqrs = Cesium.Matrix4.fromTranslationQuaternionRotationScale;
      var srtMatrix = cmftqrs(Cesium.Cartesian3.ZERO, rotationQuat, ccfe(width, height), scratchSrtMatrix);

      var modelMatrix = Cesium.Matrix4.multiply(enuMatrix, srtMatrix, srtMatrix);

      if (typeof result === 'undefined') {
        result = [];
      }

      localPositions.forEach(function(lp, index) {
        if (typeof result[index] === 'undefined') {
          result[index] = new Cesium.Cartesian3();
        }

        Cesium.Matrix4.multiplyByPoint(modelMatrix, lp, result[index]);
      });

      return result;
    }

    return getRectangleWorldPositions;
  }();

  var scratchCartesian = void 0;

  function createGroundRectangle(center, width) {
    var height = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : width;
    var originX = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0.5;
    var originY = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0.5;
    var rotation = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0.0;
    var image = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : '';

    var color = arguments[7];
    var classificationType = arguments[8];

    if (typeof center === 'undefined' || typeof width === 'undefined') {
      console.error('必须设置center和width！');
      return undefined;
    }

    scratchCartesian = scratchCartesian || new Cesium.Cartesian3();
    var cartesian = Cesium.Cartesian3.fromRadians(center[0], center[1], center[2], undefined, scratchCartesian);

    height = height || width;
    rotation = rotation || 0.0;
    // color = color || Cesium.Color.WHITE;
    color = color || new Cesium.Color(1, 1, 1, 1);
    image = image || '';
    var worldPositions = getRectangleWorldPositions(cartesian, width, height, originX, originY, rotation);
    var material = new Cesium.Material({
      fabric: {
        type: 'Image',
        uniforms: {
          image,
          color
        }
      }
    });

    // var _material$uniforms$co;
    // (_material$uniforms$co = material.uniforms.color).xbsjFromArray.apply(_material$uniforms$co, [...color]);
    return new Cesium.GroundPrimitive({
      geometryInstances: new Cesium.GeometryInstance({
        geometry: new Cesium.PolygonGeometry({
          polygonHierarchy: new Cesium.PolygonHierarchy(worldPositions),
          stRotation: -rotation
        })
      }),
      appearance: new Cesium.MaterialAppearance({
        material
      }),
      classificationType,
      asynchronous: false
    });
  }

  return createGroundRectangle;
  /***/
})();

export default createGroundRectangle;
